package com.jic.point.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.PageResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.exception.AppRuntimeException;
import com.jic.point.mapper.ConfThirdPartApplyMapper;
import com.jic.point.mapper.ConfThirdPartMapper;
import com.jic.point.model.entity.ConfThirdPart;
import com.jic.point.model.entity.ConfThirdPartApply;
import com.jic.point.service.ConfThirdPartApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 接口实现类
 *
 * @author : gym
 * @version : v1.0.0
 * @email :
 * @since : 2020-10-22 10:12:20
 */
@Service
@Slf4j
public class ConfThirdPartApplyServiceImpl implements ConfThirdPartApplyService{

    @Resource
    private ConfThirdPartApplyMapper mapper;

    @Resource
    private ConfThirdPartMapper confMapper;

    /**
     * 新增记录
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存成功
     */
    @Override
    public int save(ConfThirdPartApply request) {
        request.setAuditSts(-1);//草稿
        int flag = mapper.insert(request);
        if (flag == 1) {
            return 1;
        }else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增失败");
        }
    }

    @Override
    public int insertSelective(ConfThirdPartApply request) {
        int flag = mapper.insertSelective(request);
        if(flag == 1 ){
            return  1;
        }else{
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增失败");
        }

    }
    /**
     * 保存或修改记录信息
     * @param request 需要保存的入参实体类
     * @return int 是否保存或修改成功
     * author tzc
     * @date 2020-3-11 20:54:09
     */
    @Override
    public int saveOrUpdate(ConfThirdPartApply request){
        log.info("保存或修改记录信息，入参 = {}", JSONObject.toJSONString(request));
        try {
            if (request == null) {
                throw new AppRuntimeException(ResponseCode.EXCEPTION,"入参为空");
            }
            if (request.getId() != null) {
                log.info("保存或修改记录信息，修改，入参 = {}", JSONObject.toJSONString(request));
                request.setUpdater(request.getAdminOperate().getOperator());//更新用户—当前登陆用户id
                request.setUpdateTime(new Date());//更改时间 获取系统当前时间

                request.setAuditSts(0);//待审核
                int flag = mapper.updateByPrimaryKey(request);
                if(flag == 1){
                    return 1;
                }
            }else{
                log.info("保存或修改记录信息，保存，入参 = {}", JSONObject.toJSONString(request));
                request.setCreator(request.getAdminOperate().getOperator());//创建用户—当前登陆用户id
                request.setCreateTime(new Date());//创建时间 获取系统当前时间
                request.setAuditSts(0);//状态改为0
                int flag = mapper.updateByPrimaryKeySelective(request);
                if(flag == 1){
                    return 1;
                }else{
                    throw new AppRuntimeException(ResponseCode.EXCEPTION,"修改失败");
                }
            }
        } catch (Exception e) {
            log.error("用户保存或修改失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"用户保存或修改失败");
        }
        return -1;
    }



    /**
     * 删除记录
     *
     * @param request 需要删除,含有主键的入参实体类
     * @return int 是否删除成功
     */
    @Override
    public int deleteByPrimaryKey(ConfThirdPartApply request) {
        int flag = mapper.deleteByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        }else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除失败");
        }
    }


    /**
     * 逻辑删除记录
     *
     * @param request 含有主键的逻辑删除实体类
     * @return int 是否逻辑删除成功
     */
    @Override
    public int deleteByPrimaryKeyLogically(ConfThirdPartApply request) {
/*        request.setDeleteFlag(1);
        request.setCurrentAction(2);*/
        int flag = mapper.updateByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        }else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除记录失败");
        }
    }


    /**
     * 修改
     *
     * @param request
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(ConfThirdPartApply request){
        try {
            log.info("修改记录");
            ConfThirdPartApply param = new ConfThirdPartApply();
            BeanUtils.copyProperties(request, param);
            param.setAuditSts(-1);//提交----状态为草稿
            int flag = mapper.updateByPrimaryKeySelective(param);
            if (flag == 1) {
                return 1;
            }else {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
            }
        } catch (Exception e) {
            log.error("修改记录失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION,"修改记录失败");
        }

    }

    /**
     * 记录详情
     *
     * @param request 查询的入参实体类
     * @return 数据库查询到的实体类
     */
    @Override
    public ConfThirdPartApply getByPrimaryKey(ConfThirdPartApply request) {
        return mapper.selectByPrimaryKey(request);
    }



    /**
     * 分页查询
     *
     * @param request 分页查询入参实体类
     * @return 含有分页信息的实体类列表
     */
    @Override
    public PageInfo<ConfThirdPartApply> listPageBySelective(ConfThirdPartApply request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<ConfThirdPartApply> list = mapper.listPageBySelective(request);
        PageInfo pageInfo = new PageInfo(list);
        PageResult<ConfThirdPartApply> result = new PageResult<>();
        result.setTotal(pageInfo.getTotal());
        result.setRows(list);
        return new PageInfo(list);
    }

    /**
     * 不分页查询
     *
     * @param request 不分页查询入参实体类
     * @return 数据库查到的实体类列表
     */
    @Override
    public List<ConfThirdPartApply> listAllRecord(ConfThirdPartApply request) {
        //分页处理
        request.setDeleteFlag(0);
        return mapper.selectBySelective(request);
    }

    @Override
    public List<ConfThirdPartApply> selectBySelective(ConfThirdPartApply request) {
        return mapper.selectBySelective(request);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int auditAubmit(ConfThirdPartApply request) {
        if(request.getAuditSts() == 2){
            //审核失败
            // 修改 / 删除 审核失败  主表逻辑删除取消，申请表逻辑删除。
            int flag = deleteByPrimaryKeyLogically(request);
            ConfThirdPart confThirdPart = new ConfThirdPart();
            //主表根据 code逻辑删除
            confThirdPart.setCode(request.getCode());
            confThirdPart.setDeleteFlag(1);
            confThirdPart.setAuditReason(request.getAuditReason());
            int flag1 = confMapper.deleteFlagTemplate(confThirdPart);
            return  flag > 0 & flag1 > 0 ? 1 : 0;

        }else{//审核成功
            if(request.getCurrentAction() == 0 ){
                ConfThirdPart confThirdPart = new ConfThirdPart();
                BeanUtils.copyProperties(request, confThirdPart);
                confThirdPart.setId(null);
                confThirdPart.setAuditorId(request.getAdminOperate().getOperator());
                int flag = confMapper.insertSelective(confThirdPart);//插入到主表
                int flag1 = deleteByPrimaryKeyLogically(request);//申请表逻辑删除
                return flag > 0 & flag1 > 0 ? 1 : 0 ;
            }else if(request.getCurrentAction() == 1){
                //修改
                ConfThirdPart confThirdPart = new ConfThirdPart();
                BeanUtils.copyProperties(request, confThirdPart);
                int flag = confMapper.updateByPrimaryKeyApply(confThirdPart);
                //申请表逻辑删除
                request.setDeleteFlag(1);
                int flag1 = mapper.updateByPrimaryKey(request);
                return flag > 0 & flag1 > 0 ? 1 : 0 ;
            }else {
                //删除失败，申请表逻辑删除
                int flag = deleteByPrimaryKeyLogically(request);
                return flag;
            }
        }
    }
}
